package com.study.talk.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.talk.module.entity.ApplyJoinGroupRecord;
import com.study.talk.module.entity.GroupInfo;
import com.study.talk.module.mapper.ApplyJoinGroupRecordMapper;
import com.study.talk.module.service.ApplyJoinGroupRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class ApplyJoinGroupRecordServiceImpl extends ServiceImpl<ApplyJoinGroupRecordMapper, ApplyJoinGroupRecord> implements ApplyJoinGroupRecordService {
    @Override
    public void addRecord(GroupInfo groupInfo, Integer applyUserId) {
        ApplyJoinGroupRecord record = new ApplyJoinGroupRecord(groupInfo, applyUserId);
        save(record);
    }

    @Override
    public void removeRecord(Integer groupId, Integer userId) {
        QueryWrapper<ApplyJoinGroupRecord> wrapper = new QueryWrapper<>();
        remove(wrapper.eq("group_id", groupId).eq("apply_user_id", userId));
    }

    @Override
    public boolean hasRecord(Integer groupId, Integer userId) {
        QueryWrapper<ApplyJoinGroupRecord> wrapper = new QueryWrapper<>();
        return ObjectUtils.isNotNull(getOne(wrapper.eq("group_id", groupId).eq("apply_user_id", userId)));
    }
}
